package com.hzhr.iot.device.service.helper;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class User_DeptServiceHelper {
    @Autowired
    User_CompanyService user_companyService;
    @Autowired
    User_DeptService user_deptService;


    public String getDeptResourceCode(Map<String, String> header, Integer companyId, Integer deptId)
            throws ServiceException {
        String deptResourceCode = null;
        if (deptId == 0) {
            Map<String, Object> getCompanyQuery = new HashMap<>();
            getCompanyQuery.put("_equal_id", companyId);
            getCompanyQuery.put("_show", "one");
            getCompanyQuery.put("_show_info", true);
            deptResourceCode = user_companyService.get(header, getCompanyQuery).getModel().getResourceCode();
        } else {
            Map<String, Object> getDeptQuery = new HashMap<>();
            getDeptQuery.put("_equal_id", deptId);
            getDeptQuery.put("_show", "one");
            getDeptQuery.put("_show_info", true);
            deptResourceCode = user_deptService.get(header, getDeptQuery).getModel().getResourceCode();
        }

        return deptResourceCode;
    }

    public String getCodeById(Map<String, String> header, int id) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_id", id);
        DeptModel dept = user_deptService.get(header, query).getModel();
        if (dept == null) return null;
        return dept.getCode();
    }

    public String getRourceCodeById(Map<String, String> header, int id) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_id", id);
        DeptModel dept = user_deptService.get(header, query).getModel();
        if (dept == null) return null;
        return dept.getResourceCode();
    }
}
